package edu.gatech.asthma.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Create database and the tables
 */
public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "asthma_user.db";
	private static final String DATABASE_CREATE_TABLE_PROFILE = "create table if not exists user_profile ('identifier' integer, 'name' text, 'gender' integer, " +
			"'birthday' text, 'weight' integer, 'weight_unit' integer, 'height' integer, 'height_unit' integer, 'ethnicity' integer, 'bg_color' integer, 'tint_color' integer, 'accent_color' integer, 'pic' integer, 'best' real);";
	private static final String DATABASE_CREATE_TABLE_SYMPTOMS = "create table if not exists symptom_record ('_id' integer primary key AUTOINCREMENT, 'identifier' integer, 'time' text, 'symptoms' text);";
	private static final String DATABASE_CREATE_TABLE_TESTS = "create table if not exists test_result ('identifier' integer, 'resistance' real, 'reactance' real, 'date' text);";
	private static final String DATABASE_CREATE_TABLE_EMERGENCY = "create table if not exists emergency_contact ('name' text, 'number' text);";
	public static final int DATABASE_VERSION = 1;
	
	//Constructor
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * Execute pre-defined queries to create tables
	 */
	public void onCreate(SQLiteDatabase db)	{
		db.execSQL(DATABASE_CREATE_TABLE_PROFILE);
		db.execSQL(DATABASE_CREATE_TABLE_SYMPTOMS);
		db.execSQL(DATABASE_CREATE_TABLE_TESTS);
		db.execSQL(DATABASE_CREATE_TABLE_EMERGENCY);
	}
	
	/**
	 * Update database version
	 */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    
		if (newVersion > oldVersion) {
			Log.v("Database Upgrade", "Database version higher than old.");
		db.execSQL("drop table if exists user_profile;");
		onCreate(db);
		}
	}
}